package com.junzhiit.nengji.cms.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.bo.LabelBO;
import com.junzhiit.nengji.bo.LabelListBO;
import com.junzhiit.nengji.cms.service.LabelService;
import com.junzhiit.nengji.cms.web.exception.LabelIsExistException;
import com.junzhiit.nengji.cms.web.exception.LabelNotFoundException;
import com.junzhiit.nengji.cms.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.LabelAddVO;
import com.junzhiit.nengji.cms.web.vo.LabelEditVO;
import com.junzhiit.nengji.cms.web.vo.LabelListVO;
import com.junzhiit.nengji.cms.web.vo.LabelTreeVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.dao.GoodsLabelItemsDAO;
import com.junzhiit.nengji.dao.LabelDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.SystemTypeLabelDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.GoodsLabelItems;
import com.junzhiit.nengji.model.Label;
import com.junzhiit.nengji.model.LabelItems;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.SystemTypeLabel;
import com.junzhiit.nengji.model.enums.LoginType;

@Service
public class LabelServiceImpl extends BaseServiceImpl implements LabelService{
	
	@Autowired
	private LabelDAO labelDAO;
	
	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;

	@Autowired
	private ShopDAO shopDAO;
	
	@Autowired
	private SystemTypeLabelDAO systemTypeLabelDAO;
	
	@Autowired
	private GoodsLabelItemsDAO goodsLabelItemsDAO;
	
	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp,LabelListVO labelListVO){
		
		Label label=new Label();
		BeanUtils.copyProperties(labelListVO, label);
		
		List<LabelListBO> list = labelDAO.findPageAll(label, dtp.getStart(), dtp.getLength());
		int totalCount = labelDAO.getTotalCount(label);
		
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}
	
	@Override
	public ResultVO findById(String id){
		ResultVO result=new ResultVO();
		LabelListBO labelListBO=labelDAO.findById(id);
		if(labelListBO==null){
			throw new LabelNotFoundException("属性参数未找到");
		}

		LabelEditVO labelEditVO=new LabelEditVO();
		BeanUtils.copyProperties(labelListBO, labelEditVO);
		
		String systemTypeId=labelListBO.getSystemTypeId();
		if(StringUtils.isNotBlank(systemTypeId)){
			List<String> list=Arrays.asList(systemTypeId.split(","));
			labelEditVO.setSystemTypeId(list);
		}
		
		result.setData(labelEditVO);
		
		return result;
	}
	
	@Override
	public ResultVO add(LabelAddVO labelAddVO){
		ResultVO result=new ResultVO();
		
		Label label=new Label();
		BeanUtils.copyProperties(labelAddVO, label);
		
		if(LoginType.SHOP.toIntType()==labelAddVO.getPlatform().intValue()){
			
			SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
					.getHash(DIYKey.getSessionHashKey(labelAddVO.getSessionId()), SystemUserSessionVO.class);
			Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
			if (shop == null) {
				throw new ShopNotFoundException("店铺不存在");
			}
			
			label.setShopId(shop.getId());
		}
		
		
		int count=labelDAO.getIsExist(label);
		if(count > 0){
			throw new LabelIsExistException("参数已经存在");
		}
		
		Label data=labelDAO.insert(label);
		
		//
		List<SystemTypeLabel> systemTypeLabelList=new ArrayList<SystemTypeLabel>();
		List<String> systemTypeIds=labelAddVO.getSystemTypeId();
		for(String systemTypeId:systemTypeIds){
			SystemTypeLabel systemTypeLabel=new SystemTypeLabel();
			systemTypeLabel.setSystemTypeId(systemTypeId);
			systemTypeLabel.setLabelId(data.getId());
			systemTypeLabel.setCreatedTime(new Date());
			systemTypeLabelList.add(systemTypeLabel);
		}
		
		systemTypeLabelDAO.batchInsert(systemTypeLabelList);
		
		result.setData(data);
		
		return result;
	}
	
	
	@Override
	public ResultVO edit(String id,LabelEditVO labelEditVO){
		ResultVO result=new ResultVO();
		
		Label label=labelDAO.query(id);
		if(label==null){
			throw new LabelNotFoundException("属性参数未找到");
		}
		
		BeanUtils.copyProperties(labelEditVO, label);
		
		labelDAO.update(label);
		
		//
		List<SystemTypeLabel> systemTypeLabelList=new ArrayList<SystemTypeLabel>();
		List<String> systemTypeIds=labelEditVO.getSystemTypeId();
		for(String systemTypeId:systemTypeIds){
			SystemTypeLabel systemTypeLabel=new SystemTypeLabel();
			systemTypeLabel.setLabelId(label.getId());
			systemTypeLabel.setSystemTypeId(systemTypeId);
			systemTypeLabel.setCreatedTime(new Date());
			systemTypeLabelList.add(systemTypeLabel);
		}
		systemTypeLabelDAO.deleteByLabelId(label.getId());
		systemTypeLabelDAO.batchInsert(systemTypeLabelList);
		
		
		return result;
	}

	@Override
	public ResultVO getAll(LabelListVO labelListVO) {
		
		ResultVO result=new ResultVO();
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(labelListVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		List<LabelBO> list=labelDAO.getAll(shop.getId(),labelListVO.getSystemTypeId());
		
		Collection<LabelTreeVO> data=toCollection(list);
		
		result.setData(data);
		
		return result;
	}
	
	
	private Collection<LabelTreeVO> toCollection(List<LabelBO> LabelBOs){
		Map<String, LabelTreeVO> map = new LinkedHashMap<String, LabelTreeVO>();
		
		for (LabelBO labelBO : LabelBOs) {
			
			LabelTreeVO rbo = map.get(labelBO.getLabelId());
	        if (rbo==null) {
	        	rbo = new LabelTreeVO();
	        	rbo.setId(labelBO.getLabelId());
	        	rbo.setName(labelBO.getName());
	            List<LabelItems> childrens = new ArrayList<LabelItems>();
	            rbo.setChildrens(childrens);
	            map.put(labelBO.getLabelId(),rbo);
	        }
	        
	        LabelItems labelItems=new LabelItems();
	        BeanUtils.copyProperties(labelBO, labelItems);
	        rbo.getChildrens().add(labelItems);
	    }
		
		Collection<LabelTreeVO> data = map.values();
		
		return data;
	}
	
	@Override
	public ResultVO updateTodelete(String id){
		ResultVO result=new ResultVO();
		
		Label label=labelDAO.query(id);
		if(label==null){
			throw new LabelNotFoundException("属性参数未找到");
		}
		labelDAO.updateToDelete(id);
		
		GoodsLabelItems goodsLabelItems=new GoodsLabelItems();
		goodsLabelItems.setLabelId(id);
		goodsLabelItemsDAO.deleteLabel(goodsLabelItems);
		
		return result;
	}
	
}
